package com.mhg.reflect;

import com.mhg.collection.bean.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


//1.获取构造方法 创建对象
public class Demo_02 {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		//1.反射的入口 Class 字节码文件对象
		Class<?> clazz = Class.forName("com.demo1.reflect.Person");
		//2.创建 Person 对象 可以使用两种方式
		//无参构造
		Person p = (Person)clazz.newInstance();
		//Person p = new Person();
		p.setName("张三");
		System.out.println(p.getName());
		
		//有参构造
		//Person p2 = new Person("李四",24);
		Constructor<?> c = clazz.getConstructor(int.class,String.class);
		//使用Constructor类中 newInstance(...) 方法 创建对象
		Person p2 = (Person)c.newInstance(24,"王五");
		System.out.println(p2.getName()+"::"+p2.getAge());
	}

}